package com.example.sqlite.activities;


import com.example.sqlite.dao.ContactsDAO;
import com.example.sqlite.models.Contact;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class EditContactActivity extends Activity {

	private ContactsDAO dao;
	private Contact contact;
	private Button buttonEditContact;
	private EditText editTextName;
	private EditText editTextFone;

	private final OnClickListener onClickListenerButtonEditContact = new OnClickListener() {

		@Override
		public void onClick(View view) {
			dao.edit(contact.getId(),
					editTextName.getEditableText().toString(), editTextFone
							.getEditableText().toString());
			finish();
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edit_contact);

		Intent intent = getIntent();
		long id = intent.getExtras().getLong("id");
		dao = new ContactsDAO(this);
		dao.open();

		contact = dao.get(id);
		buttonEditContact = (Button) findViewById(R.id.button_add);
		editTextName = (EditText) findViewById(R.id.edit_text_name);
		editTextFone = (EditText) findViewById(R.id.edit_text_fone);

		editTextName.setText(contact.getName());
		editTextFone.setText(contact.getFone());

		buttonEditContact.setOnClickListener(onClickListenerButtonEditContact);

	}

	@Override
	protected void onResume() {
		dao.open();
		super.onResume();
	}

	@Override
	protected void onPause() {
		dao.close();
		super.onPause();
	}

}
